<?php
class Lang_general_model extends CI_Model {
	public $table = 'lang_general';
	public $id = 0;
	public $key = '';
	public $ro = '';
	public $hu = '';
	public $en = '';
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	public function get($id) {
		$result = $this->db->select ()->from ( $this->table )->where ( 'id', $id )->get ()->row ();
		$obj = FALSE;
		if ($result) {
			$obj = new Lang_general_model ( );
			$obj->id = $result->id;
			$obj->key = $result->key;
			$obj->ro = $result->ro;
			$obj->hu = $result->hu;
			$obj->en = $result->en;
		}
		return $obj;
	}
	
	public function getAll($filter = array()) {
		if (isset ( $filter ['field'] ) and strlen ( $filter ['field'] )) {
			$this->db->select ( $filter ['field'] );
		} else {
			$this->db->select ();
		}
		$this->db->from ( $this->table );
		if (isset ( $filter ['name_filter'] ) and strlen ( $filter ['name_filter'] )) {
			$this->db->like ( $this->table . '.' . $this->config->item ( 'language_abbr' ), $filter ['name_filter'] );
		}
		if (isset ( $filter ['ro'] ) and strlen ( $filter ['ro'] )) {
			$this->db->where ( 'ro', $filter ['ro'] );
		}
		if (isset ( $filter ['hu'] ) and strlen ( $filter ['hu'] )) {
			$this->db->where ( 'hu', $filter ['hu'] );
		}
		if (isset ( $filter ['key'] ) and strlen ( $filter ['key'] )) {
			$this->db->where ( 'key', $filter ['key'] );
		}
		if (isset ( $filter ['key_in'] ) and strlen ( $filter ['key_in'] )) {
			$this->db->like ( 'key', $filter ['key_in'] );
		}
		if (isset ( $filter ['id_not_equal'] ) and is_numeric ( $filter ['id_not_equal'] )) {
			$this->db->where ( 'id !=', $filter ['id_not_equal'] );
		}
		if (isset ( $filter ['by_value'] ) and is_array($filter ['by_value'] ) && count($filter['by_value']) == 2) {
			$this->db->like ( $filter['by_value'][0], $filter ['by_value'][1] );
		}
		$order = 'key';
		$this->db->order_by ( $order );
		$query = $this->db->get ();

		return $query;
	}
	
	public function getByParams($limit, $dir = 'ASC', $sort = 'key', $start, $filter = array()) {
		$this->db->select ()->order_by ( $sort, $dir );
		if (isset ( $filter ['name_filter'] ) and strlen ( $filter ['name_filter'] )) {
			$this->db->like ( $this->table . '.val_' . $this->config->item ( 'language_abbr' ), $filter ['name_filter'] );
		}
		if (isset ( $filter ['ro'] ) and strlen ( $filter ['ro'] )) {
			$this->db->where ( 'ro', $filter ['ro'] );
		}
		if (isset ( $filter ['en'] ) and strlen ( $filter ['en'] )) {
			$this->db->where ( 'en', $filter ['en'] );
		}
		if (isset ( $filter ['key'] ) and strlen ( $filter ['key'] )) {
			$this->db->where ( 'key', $filter ['key'] );
		}
		if (isset ( $filter ['key_in'] ) and strlen ( $filter ['key_in'] )) {
			$this->db->like ( 'key', $filter ['key_in'] );
		}
		$query = $this->db->get ( $this->table, $limit, $start );
		return $query;
	}
	
	public function insert() {
		$data = array (
			'en' => $this->en, 
			'ro' => $this->ro, 
			'hu' => $this->hu,
			'key' => $this->key 
			);
		$this->db->insert ( $this->table, $data );
	}
	
	public function update() {
		$data = array (
			'en' => $this->en, 
			'ro' => $this->ro, 
			'hu' => $this->hu,
			'key' => $this->key 
			);
		$this->db->where ( 'id', $this->id );
		$this->db->update ( $this->table, $data );
	}
	
	/**
	 * Delete entry from db.
	 * @param mixed $filter
	 * @return bool
	 */
	public function delete($filter = array()){
		if (!count($filter)){
			$this->db->where('id', $this->id);
		}
		else{
		}
		$this->db->delete($this->table);
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	public function getForTranslate($filter = array()) {
		if (isset ( $filter ['field'] ) and strlen ( $filter ['field'] )) {
			$this->db->select ( $filter ['field'] );
		} 
		else {
			$this->db->select ();
		}
		$this->db->from ( $this->table );
		$query = $this->db->get ();
		return $query;
	}
}